#include "StdAfx.h"
#include "LayoutNode.h"
#include "FakeNode.h"

namespace fusion
{
	namespace mode10
	{
		LayoutNode::LayoutNode(Node* node, Ref<BaseLayoutParams> params):
			m_node(node), 
			m_params(params),
			m_parent(NULL),
			m_MeasuredWidth(0),
			m_MeasuredHeight(0),
			m_LayoutDirty(false)
		{
			//ASSERT(m_node);
			//ASSERT(m_params);
			m_node->SetRelatedLayout(this);
			m_NodeID = m_node->getID();
		};

		LayoutNode::~LayoutNode()
		{

		}

		int LayoutNode::GetNodeName() 
		{
			return m_node->getName(); 
		}

		void LayoutNode::Measure(const Ref<MeasureSpec>& widthSpec, const Ref<MeasureSpec>& heightSpec)
		{
			onMeasure(widthSpec, heightSpec);
		}
		void LayoutNode::Layout()
		{
			onLayout();
		}

		Ref<LayoutNode> LayoutNode::GetParent()
		{
			return m_parent;
		}
		void LayoutNode::SetParent(const Ref<LayoutNode>& parent)
		{
			m_parent = parent;
		}

		void LayoutNode::RequestLayout()
		{
			if (m_parent)
			{
				m_parent->RequestLayout();
			}
			m_LayoutDirty = true;
		}
		//bool LayoutNodeWrap::AddLayoutNode(Node* node, Ref<BaseLayoutParams> params)
		//{
		//	//ASSERT(node);
		//	//ASSERT(params);

		//	if (!node || !params)
		//		return false;

		//	return onAddLayoutNode(node, params);
		//}
		void LayoutNode::ReplaceParams(Ref<BaseLayoutParams>& params)
		{
			m_params = params;
		}

		int LayoutNode::getSuggestedMinimumHeight()
		{
			return m_node->sizeY;
		}
		int  LayoutNode::getSuggestedMinimumWidth()
		{
			return m_node->sizeX;
		}

		void LayoutNode::onMeasure(const Ref<MeasureSpec>& widthSpec, const Ref<MeasureSpec>& heightSpec)
		{
			int measuredWidth = getDefaultSize( getSuggestedMinimumWidth(), widthSpec);
			int measuredHeight = getDefaultSize( getSuggestedMinimumHeight(), heightSpec);
			setMeasuredDimension( measuredWidth, measuredHeight );
		}

		int LayoutNode::getDefaultSize(int size, const Ref<MeasureSpec>& measureSpec)
		{
			int result = size;
			switch (measureSpec->Mode)
			{
			case MeasureSpec::UNSPECIFIED:
				result = size;
				break;
			case MeasureSpec::AT_MOST:
			case MeasureSpec::EXACTLY:
				result = measureSpec->Size;
				break;
			default:
				break;
			}
			return result;
		}

		void LayoutNode::setMeasuredDimension(int measuredWidth, int measuredHeight)
		{
			m_MeasuredWidth = measuredWidth;
			m_MeasuredHeight = measuredHeight;
		}
	}
}